<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
    <title>注册新用户</title>
    <meta name="decorator" content="default"/>
    <script type="text/javascript">
        $(document).ready(function() {
                layui.use(['laypage','layedit', 'form'], function(){
                    form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                });
                
                var data = ${fns:toJson(user)};
                if(!data||!data.isNewRecord){
                    var internetStr = data.roleNames;
                    if(internetStr){
                        var internetArr = internetStr.split(",");
                        for(var i=0;i<internetArr.length;i++){
                            $('input[title='+internetArr[i]+']').attr("checked",true);
                        }
                        form.render("checkbox")
                    }
                }
            $("#inputForm").validate({
                rules: {
                    loginName: {remote: "${ctx}/checkLoginName?oldLoginName=" + encodeURIComponent('${user.loginName}')}
                },
                messages: {
                    loginName: {remote: "用户登录名已存在"},
                    confirmNewPassword: {equalTo: "输入与上面相同的密码"}
                },
                submitHandler: function(form){
                    $('#btnSubmit').blur();//去除提交按钮焦点  防止提示错误点击空格 及回车 后继续提交
                    //电话号验证
                    function checkContactNumber(number) {  
                        var isPhone = /^0\d{2,3}-?\d{7,8}$/;
                        var isMobile =/^(((1[3-9]{1}[0-9]{1}))+\d{8})$/;  
                        //如果为1开头则验证手机号码  
                        if (number.substring(0, 1) == 1) {  
                            if (!isMobile.exec(number)) {  
                                return false;  
                            }  
                        }  else if (number.substring(0, 1) == 0) {  
                            if (!isPhone.test(number)) {   
                                return false;  
                            }  
                        }  else {  
                            return false;  
                        } 
                        return true;  
                    };
                    if($("#phone").val() != "" && !checkContactNumber($("#phone").val())){
                        layer.alert("联系电话输入错误，请正确填写！", {icon: 0});
                         return false;
                     }
                    if($("#mobile").val() != "" && !checkContactNumber($("#mobile").val())){
                        layer.alert("手机号输入错误，请正确填写！", {icon: 0});
                         return false;
                     }
                    if($('#confirmPassword').val() !== $('#newPassword').val()){
                    	layer.alert("两次密码输入不一致！", {icon: 0});
                        return false;
                    }
                    //选择用户角色
                    var isChecked = false;
                    var roles = "";
                    $("input[name='roleIdList']").each(
                        
                        function(){
                            if($(this).get(0).checked){
                            	if($(this).val()=='cf24f01ee9a64152bd481e7f21edef25'){
                            		$("#office").val("87c49eef5320422d944ae9752aa67242");
                            	}else if($(this).val()=='e56f1778890945ee92d113c5d808ce19'){
                            		$("#office").val("571e6dd91e2946d49ca226a117fdfc58");
                            	}else{
                            		$("#office").val("e83093533f364a259e9d52932a849828");
                            	}
                                roles += $(this).val() + ",";
                              isChecked = true;
                            }
                        });
                    if(!isChecked){
                         layer.alert('请选择用户角色！', {icon: 0});
                         return;
                    }else{
                        $("#role").val(roles);
                    }
                    loading('正在提交，请稍等...');
                    form.submit();
                },
                errorContainer: "#messageBox",
                errorPlacement: function(error, element) {
                    $("#messageBox").text("输入有误，请先更正。");
                    if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
                        error.appendTo(element.parent().parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            });
        });
         
    </script>
</head>
<body>
    <form:form id="inputForm" modelAttribute="user" action="${ctx}/register" method="post" class="layui-form">
        <form:hidden path="id"/>
        <sys:message content="${message}"/>
        <%-- 
        <div class="layui-form-item">
            <div class="layui-inline">
            <label class="layui-form-label">头像:</label>
            <div class="layui-input-inline">
                <form:hidden id="nameImage" path="photo" htmlEscape="false" maxlength="255" class="input-xlarge"/>
                <sys:ckfinder input="nameImage" type="images" uploadPath="/photo" selectMultiple="false" maxWidth="100" maxHeight="100"/>
            </div>
            </div>
        </div>
        --%>
        <input id="loginFlag" name="loginFlag" type="hidden" value='1'/>
        <input id="company" name="company.id" type="hidden" value='a0e84182750a409e9bf73971144c5f37'/>
        <input id="office" name="office.id" type="hidden" value='e83093533f364a259e9d52932a849828'/>
        <div class="layui-form-item">
            <div class="layui-inline">
            <label class="layui-form-label">姓名:</label>
            <div class="layui-input-inline">
                <form:input path="name" htmlEscape="false" maxlength="50" class="required layui-input"/>
            </div>
                <span class="layui-form-mid"><font color="red">*</font> </span>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
            <label class="layui-form-label">登录名:</label>
            <div class="layui-input-inline">
                <input id="oldLoginName" name="oldLoginName" type="hidden" value="${user.loginName}" >
                <form:input path="loginName" htmlEscape="false" maxlength="50" class="layui-input required userName"/>
            </div>
                <span class="layui-form-mid"><font color="red">*</font> </span>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
            <label class="layui-form-label">密码:</label>
            <div class="layui-input-inline">
                <input id="newPassword" name="newPassword" type="password" value="" maxlength="50" minlength="3" class="layui-input required"/>
            </div>
                <span class="layui-form-mid"><font color="red">*</font> </span>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
            <label class="layui-form-label">确认密码:</label>
            <div class="layui-input-inline">
                <input id="confirmPassword" name="confirmPassword" type="password" value="" maxlength="50" minlength="3" equalTo="#newPassword" class="layui-input"/>
            </div>
            <span class="layui-form-mid"><font color="red">*</font> </span>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
            <label class="layui-form-label">邮箱:</label>
            <div class="layui-input-inline">
                <form:input path="email" htmlEscape="false" maxlength="100" class="layui-input" lay-verify="email"/>
            </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
            <label class="layui-form-label">电话:</label>
            <div class="layui-input-inline">
                <form:input path="phone" id="phone" htmlEscape="false" maxlength="100" class="layui-input" />
            </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
            <label class="layui-form-label">手机:</label>
            <div class="layui-input-inline">
                <form:input path="mobile" id= "mobile" htmlEscape="false" maxlength="100" class="layui-input" />
            </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户角色:</label>
            <div class="layui-input-block">
            <input type="hidden" id="roleList" >
                <c:forEach items="${allRoles}" var="dict" varStatus="dictStatus">
                    <c:if test="${dict.id != '1' && dict.id != '2'}">
                        <span><input id="rollList${dictStatus.index}" name="roleIdList" type="checkbox" value="${dict.id}"  data-value="" title="${dict.name}" lay-verify="required"></span>
                    </c:if>
                </c:forEach><span class="layui-form-right"><font color="red">* 注：此处为必选项</font></span>&nbsp;
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-inline">
                <form:textarea path="remarks" htmlEscape="false" rows="3" maxlength="200" class="layui-textarea"/>
            </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
            <input id="btnSubmit" class="layui-btn" type="submit" value="保 存"/>&nbsp;
            <input id="btnCancel" class="layui-btn layui-btn-primary" type="button" value="返 回" onclick="history.go(-1)"/>
            </div>
        </div>
    </form:form>
</body>
</html>